package com.ethink.warehouse.manager;

import com.ethink.warehouse.api.enums.LogisticsTraceDetailOrderTypeEnum;
import com.ethink.warehouse.api.enums.SubjectEnum;
import com.ethink.warehouse.api.info.LogisticsTransferDetailOrderInfo;
import com.ethink.warehouse.api.info.LogisticsTransferOrderInfo;
import com.ethink.warehouse.api.request.LogisticsTraceApplyAddRequest;
import com.ethink.warehouse.common.mapper.MateInventoryMapper;
import com.ethink.warehouse.service.LogisticsTraceApplyOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author default
 * @date 2022-11-10
 */
@Slf4j
@Component
public class LogisticsTraceDetailManager {

    @Autowired
    private LogisticsTraceApplyOrderService logisticsTraceApplyOrderService;

    @Autowired
    private MateInventoryMapper mateInventoryMapper;

    /**
     * 调拨审批通过，创建待出商品申请
     *
     * @param logisticsTransferOrderInfo
     * @param detailInfoList
     */
    public void dbCreateLogisticsTraceDetailOrder(LogisticsTransferOrderInfo logisticsTransferOrderInfo,
                                                  List<LogisticsTransferDetailOrderInfo> detailInfoList) {
//        List<MateInventoryDetailInfo> mateInventoryDetailInfoList = mateInventoryMapper.listMateInventoryByIdList(detailInfoList.stream()
//                .map(LogisticsTransferDetailOrderInfo::getMateInventoryId).distinct().collect(Collectors.toList()));
//        Map<Long, MateInventoryDetailInfo> mateInventoryDetailInfoMap = mateInventoryDetailInfoList.stream()
//                .collect(Collectors.toMap(MateInventoryDetailInfo::getId, info -> info));
        // 创建待入商品清单
        LogisticsTraceApplyAddRequest addRequest = new LogisticsTraceApplyAddRequest();
        addRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T2.getIntValue());
        addRequest.setSourceOrderNo(logisticsTransferOrderInfo.getOrderNo());
        addRequest.setSourceOrderType(SubjectEnum.T32.getCode());
        addRequest.setSubject(SubjectEnum.T32.getCode());
        addRequest.setCreateUserCode(logisticsTransferOrderInfo.getCreateUserCode());
        addRequest.setCreateUserName(logisticsTransferOrderInfo.getCreateUserName());
        addRequest.setCompanyCode(logisticsTransferOrderInfo.getInSubWarehouseCode());
        addRequest.setCompanyName(logisticsTransferOrderInfo.getInSubWarehouseName());
        addRequest.setSaleCkItems(detailInfoList.stream().map(detailInfo -> {
            LogisticsTraceApplyAddRequest.SaleCkItem item = new LogisticsTraceApplyAddRequest.SaleCkItem();
            item.setSourceDetailOrderNo(detailInfo.getDetailOrderNo());
            item.setApplyInQty(detailInfo.getQty());
            item.setMateInventoryId(detailInfo.getMateInventoryId());
            item.setTargetSubWarehouseCode(logisticsTransferOrderInfo.getInSubWarehouseCode());
            item.setTargetSubWarehouseName(logisticsTransferOrderInfo.getInSubWarehouseName());
            return item;
        }).collect(Collectors.toList()));
        logisticsTraceApplyOrderService.addLogisticsTraceApplyOrder(addRequest);
    }
}
